﻿namespace TcpSample.Server;

internal static class TaskExtensions
{
    internal static Task WaitAsync(this CancellationToken ct)
    {
        var cancelationTaskCompletionSource = new TaskCompletionSource<bool>();
        ct.Register(CancellationTokenCallback, cancelationTaskCompletionSource);
        return ct.IsCancellationRequested ? Task.CompletedTask : cancelationTaskCompletionSource.Task;
    }

    private static void CancellationTokenCallback(object taskCompletionSource)
    {
        ((TaskCompletionSource<bool>)taskCompletionSource).SetResult(true);
    }
}
